package com.perfectoMobile.utils.httpClient;

import java.io.IOException;

import junit.framework.SystemTestCase;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;


public class HttpEngine {
	
	private String url = "";
	

	public HttpClient client = new HttpClient();
	private GetMethod getMethod = new GetMethod();
	private PostMethod postMethod =  new PostMethod();
	private String requestbody = "";
	private URI uri =  null ;
	private String response = null ;	
	
	
	
	public HttpEngine() {
		// TODO Auto-generated constructor stub
	}
	public HttpEngine(String url) {
		// TODO Auto-generated constructor stub
		this.url = url;
	}
	
	public URI getUri() {
		return uri;
	}

	
	public String getRequestbody() {
		return requestbody;
	}
	public void setRequestbody(String requestbody) {
		this.requestbody = requestbody;
	}



	public void setGetMethod(GetMethod getMethod) {
		this.getMethod = getMethod;
	}
	public void setPostMethod(PostMethod postMethod) {
		this.postMethod = postMethod;
	}



	
	public PostMethod getPostMethod() {
		return postMethod;
	}
	
	
	public GetMethod getGetMethod() {
		return getMethod;
	}



	public String getResponse() {
		return response;
	}



	
	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}
	
	
	
	@SuppressWarnings("deprecation")
	public int executePostMethod(String url,String body) throws NoHttpResponseException,Exception {
		  int statusCode  = 0;
	        setUrl(url);
	        setRequestbody(body);
	        uri = new URI(this.url);
	        postMethod.setURI(uri);
	        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false));
	        try {
	            // Execute the method.
	        	postMethod.setRequestBody(this.requestbody);
	             statusCode = client.executeMethod(postMethod);

	            if (statusCode != HttpStatus.SC_OK) {
	              System.err.println("Method failed: " + postMethod.getStatusLine());
	            }

	            // Read the response body.
	            byte[] responseBody = postMethod.getResponseBody();

	            // Deal with the response.
	            // Use caution: ensure correct character encoding and is not binary data
	            response = new String(responseBody);

	          } catch (HttpException e) {
	            System.err.println("Fatal protocol violation: " + e.getMessage());
	            e.printStackTrace();
	          } catch (IOException e) {
	            System.err.println("Fatal transport error: " + e.getMessage());
	            e.printStackTrace();
	          } finally {
	            // Release the connection.
	        	  postMethod.releaseConnection();
	            
	          } 
			    return statusCode;
	
		
	}
	
    @SuppressWarnings("deprecation")
	public int executePostMethod(String url) throws NoHttpResponseException,Exception {

    	  int statusCode  = 0;
	        setUrl(url);
	        uri = new URI(this.url);
	        postMethod.setURI(uri);
	        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false));
	        try {
	            // Execute the method.
	             statusCode = client.executeMethod(postMethod);

	            if (statusCode != HttpStatus.SC_OK) {
	              System.err.println("Method failed: " + postMethod.getStatusLine());
	            }

	            // Read the response body.
	            byte[] responseBody = postMethod.getResponseBody();

	            // Deal with the response.
	            // Use caution: ensure correct character encoding and is not binary data
	            response = new String(responseBody);

	          } catch (HttpException e) {
	            System.err.println("Fatal protocol violation: " + e.getMessage());
	            e.printStackTrace();
	          } catch (IOException e) {
	            System.err.println("Fatal transport error: " + e.getMessage());
	            e.printStackTrace();
	          } finally {
	            // Release the connection.
	        	  postMethod.releaseConnection();
	            
	          } 
			    return statusCode;
		
	}
	
	
	
	
	
	@SuppressWarnings("deprecation")
	public int executeGetMethod(String url) {
        int statusCode  = 0;
        setUrl(url);
        try {
        uri = new URI(this.url);
        getMethod.setURI(uri);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false));

            // Execute the method.
             statusCode = client.executeMethod(getMethod);

            if (statusCode != HttpStatus.SC_OK) {
              SystemTestCase.report.report(("Method failed: " + getMethod.getStatusLine()));
            }

            // Read the response body.
            byte[] responseBody = getMethod.getResponseBody();

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
            response = new String(responseBody);
          } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
          } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
          } finally {
            // Release the connection.
        	  getMethod.releaseConnection();
            
          } 
		    return statusCode;
		
	}
	
	
	
	HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
	    public boolean retryMethod( final HttpMethod method, final IOException exception, int executionCount) {
	        if (executionCount >= 5) {
	            return false;
	        }
	        if (exception instanceof NoHttpResponseException) {
	            // Retry if the server dropped connection on us
	            return true;
	        }
	        if (!method.isRequestSent()) {
	            // Retry if the request has not been sent fully or
	            // if it's OK to retry methods that have been sent
	            return true;
	        }
	        // otherwise do not retry
	        return false;
	    }

	
	};
	

	

}
